let li2 = document.getElementById("li2");
let li1 = document.getElementById("li1");
let li3 = document.getElementById("li3");
let li4 = document.getElementById("li4");
let li5 = document.getElementById("li5");
let li6 = document.getElementById("li6");
let li7 = document.getElementById("li7");
li1.onclick = function () {
    location.href = "home.html"
}
li2.onclick = function () {
    location.href = "ReaderMana.html"
}
li3.onclick = function () {
    location.href = "BookDetails.html"
}
li4.onclick = function () {
    location.href = "BookBorrow.html"
}
li5.onclick = function () {
    location.href = "book_Info.html"
}
li6.onclick = function () {
    location.href = "BookClass.html"
}
li7.onclick = function () {
    location.href = "bookLend.html"
}




// 曹


// TODO 获取所有的学生信息
window.onload = function () {
    //获取token
    const token = sessionStorage.getItem("token")
    //发送ajax
    axios({
        url: "http://localhost:8080/admin/lendlist/querylendlist",
        methon: "get",
        headers: { token }
    })
        //向服务器拿到的数据
        .then(res => {
            // console.log(res.data.data)
            if (res.data.code == 200) {
                //分页
                //默认显示第一页
                sessionStorage.setItem("pageNo", 1)
                //一页显示8条
                sessionStorage.setItem("pageNum", 8)
                //存储所有的学生信息
                sessionStorage.setItem("studentInfo", JSON.stringify(res.data.data))

                Render(res.data.data.slice(0, 5))
            } else {
                alert("长时间不登录,请重新登录")
                location.href = "../html/login.html"
            }
        })

}

//将数据渲染到页面
function Render(data) {
    //默认显示第一页,一页显示8个
    const tbody = document.querySelector("tbody")
    tbody.innerHTML = ""
    //渲染
    data.forEach((item, index) => {
        let str = `
      <tr align="center">
          <td >${index + 1}</td>
          <td>${item.book_ref}</td>
          <td>${item.lend_time}</td>
          <td>${item.back_time}</td>
          <td>${item.state}</td>
          <td>${item.price}</td>
          <td>
              <button class="btn btn-danger btn-sm" data-id=${item.id}>删除</button>
              <button class="btn btn-warning btn-sm" data-id=${item.id} data-bs-toggle="modal"  data-bs-target="#myModal1">修改</button>
          </td>
      </tr>
      `
        tbody.innerHTML += str;
    })
}

// TODO 删除数据

const tbody = document.querySelector("tbody")
//事件委托//事件委托
tbody.addEventListener("click", (e) => {
    //删除信息,与渲染修改的模态框
    console.log(e.target.dataset.id)
    const id = e.target.dataset.id
    //获取token
    const token = sessionStorage.getItem("token")
    // 点击删除，删除数据
    if (e.target.className.includes('danger')) {
        const res = window.confirm("您确定要删除吗?")
        if (res) {
            // 发送axios
            axios({
                url: "http://localhost:8080/admin/lendlist/dellendlist",
                method: "post",
                headers: { token },
                data: {
                    id
                }
            })
                .then(res => {
                    location.reload();
                })
        }
    }

    //  渲染修改模态框
    if (e.target.className.includes('warning')) {
        axios({
            url: 'http://localhost:8080/admin/lendlist/idlendlist',
            method: 'post',
            headers: 'token',
            data: { id }
        })
            .then(res => {
                // console.log(res.data.data)
                document.getElementById("id1").value = res.data.data.reader_id;
                document.getElementById("reader1").value = res.data.data.id;
                document.getElementById("book1").value = res.data.data.book_ref;
                document.getElementById("lendtime1").value = res.data.data.lend_time;
                document.getElementById("backtime1").value = res.data.data.back_time;
                document.getElementById("price1").value = res.data.data.price;
                document.getElementById("state1").value = res.data.data.state;
            })
    }
})

// TODO 添加数据
function Add() {
    //获取数据
    const reader = document.getElementById("reader").value.trim();
    const book = document.getElementById("book").value.trim();
    const lendtime = document.getElementById("lendtime").value.trim();
    const backtime = document.getElementById("backtime").value.trim();
    const price = document.getElementById("price").value.trim();
    const state = document.getElementById("state").value.trim();

    //发送请求
    const token = sessionStorage.getItem("token")
    axios({
        url: "http://localhost:8080/admin/lendlist/addlendlist",
        method: "post",
        headers: { token },
        data: {
            reader, book, lendtime, backtime, price, state
        }
    })
        .then(res => {
            location.reload();
        })
}

// TODO 修改数据
// 第一步：渲染修改的模态框
// 第二步：修改数据，提交给数据库reader1=>后台id

function Update() {
    const id1 = document.getElementById("id1").value
    const reader1 = document.getElementById("reader1").value
    const book1 = document.getElementById("book1").value
    const lendtime1 = document.getElementById("lendtime1").value
    const backtime1 = document.getElementById("backtime1").value
    const price1 = document.getElementById("price1").value
    const state1 = document.getElementById("state1").value
  console.log(book1);
    const token = sessionStorage.getItem("token")
    // 发送axios给服务器
    axios({
        url: "http://localhost:8080/admin/lendlist/updatelendlist",
        method: "post",
        headers: { token },
        data: { id1, reader1, book1, lendtime1, backtime1, price1, state1 }
    })
        .then(res => {
            location.reload();
        })
}